<?php
class Upload {
  
  var $upload_file;     
  var $allowed_extensions;
  var $directory;
  var $max_filesize = 0;
  var $chmod_mode = 0777;
  var $upload_type;
  var $rename_file = false;
  var $out_filename = '';
  
  var $saved_filename;
  var $file_extension = '';
  var $file_size_format = '';
  var $errors;
  
  //=====================================================
  //  Upload File                  
  //=====================================================
  function upload_process()
  {
	  $file_name = $this->upload_file['name'];
	  $file_size = $this->upload_file['size'];
	  $file_type = $this->upload_file['type'];
	  
	  $extensions = explode(",", $this->allowed_extensions);
	  $this->file_extension = $this->_get_file_extension($file_name);
	  $this->file_size_format = $this->size_format($file_size);
	  
	  //-----------------------------------------
	  //  Did they upload a file?
	  //-----------------------------------------
	  if ($this->upload_file['name'] == "" || !$this->upload_file['name'] || !$this->upload_file['size'] || ($this->upload_file['name'] == "none"))
	  {
	      $this->errors[] = "Please choose a file to upload.";
		  return;
	  }
	  
	  //-----------------------------------------
	  //  Check extension
	  //-----------------------------------------
	  if (!$this->allowed_extensions)
	  {
	      $this->errors[] = "Invalid file extension.";
	  }
	  
	  if (!in_array($this->file_extension, $extensions))
      {
          $this->errors[] = 'The file type "'.$file_type.'"('.$this->file_extension.') is not allowed.';
      }
	  
	  //-----------------------------------------
	  //  Check file size
	  //-----------------------------------------
      if (($this->max_filesize) && ($file_size > $this->max_filesize ))
      {
          $this->errors[] = 'Filesize too large. The maximum filesize is '.$this->size_format($this->max_filesize).', your '.$this->upload_type.' is '.$this->file_size_format.'.';
      }	  
	  
	  //-----------------------------------------
	  //  Errors?
	  //-----------------------------------------
	  if (!isset($this->errors))
	  {
		  //-----------------------------------------
		  //  Set file name
		  //-----------------------------------------
		  if ($this->rename_file == true)
		  {
			  $this->saved_filename = substr(md5($file_name.time()), 0, 12);
		  }
		  else
		  {
			  if ($this->out_filename)
			  {
				  $this->saved_filename = $this->out_filename;
			  }
			  else
			  {
				  $file_name = preg_replace("/[^\w\.]/", "_", $file_name);
				  $this->saved_filename = str_replace( '.'.$this->file_extension, "", $file_name );
			  }
		  }
		  
		  $this->saved_filename = $this->saved_filename.'.'.$this->file_extension;
		  $upload_path = $this->directory.$this->saved_filename;
		  
		  //-----------------------------------------
		  //  Copy file
		  //-----------------------------------------
		  if ( ! @move_uploaded_file($this->upload_file['tmp_name'], $upload_path) )
		  {
		      $this->errors[] = "Your file could not be uploaded.  Please try again.";
			  return;
		  }
		  else
		  {
		      @chmod( $upload_path, $this->chmod_mode );
			  return;
		  }
	  }	 
	  else
	  {
		  return;
	  } 
  }
  
  //=====================================================
  //  Get File Extension                
  //=====================================================
  function _get_file_extension($file)
  {
      $ext = explode(".", $file);
      $extcount = count($ext);
	  return strtolower($ext[$extcount - 1]);
  }
  
  //=====================================================
  //  Size Format                
  //=====================================================
  function size_format($bytes = "")
  {
      $retval = "";
      
      if ($bytes >= 1048576) 
	  {
          $retval = round($bytes / 1048576 * 100) / 100 . 'MB';
      } 
	  elseif ($bytes >= 1024) 
	  {
          $retval = round($bytes / 1024 * 100) / 100 . 'KB';
      } 
	  else 
	  {
          $retval = $bytes . 'Bytes';
      }
      
      return $retval;
  }
  
  //==========================================================
  // check if error occurs in upload
  //==========================================================
  
  function is_error()
  {
	  $file_name = $this->upload_file['name'];
	  $file_size = $this->upload_file['size'];
	  $file_type = $this->upload_file['type'];
	  
	  
	  $extensions = explode(",", $this->allowed_extensions);
	  $this->file_extension = $this->_get_file_extension($file_name);
	  $this->file_size_format = $this->size_format($file_size);
	  
	  //-----------------------------------------
	  //  Did they upload a file?
	  //-----------------------------------------
	  
	  if ($this->upload_file['name'] == "" || !$this->upload_file['name'] || !$this->upload_file['size'] || ($this->upload_file['name'] == "none"))
	  {
		  $this->errors[] = "Please choose a file to upload.";
		  return true;
	  }
	  
	  
	  //-----------------------------------------
	  //  Check extension
	  //-----------------------------------------
	  if (!$this->allowed_extensions)
	  {
		  
	      $this->errors[] = "Invalid file extension.";
		  return true;
	  }
	  
	  
	  if (!in_array($this->file_extension, $extensions))
      {
          $this->errors[] = 'The file type "'.$file_type.'"('.$this->file_extension.') is not allowed.';
		  return true;
      }
	  
	  //-----------------------------------------
	  //  Check file size
	  //-----------------------------------------
      if (($this->max_filesize) && ($file_size > $this->max_filesize ))
      {
          $this->errors[] = 'Filesize too large. The maximum filesize is '.$this->size_format($this->max_filesize).', your '.$this->upload_type.' is '.$this->file_size_format.'.';
		  return true;			  
      }	  
	  
	  return false;
	  
  
  }
  
  
  
  
}
?>